class PerformanceMonitor {
    constructor(options = {}) {
        this.options = {
            warningThreshold: options.warningThreshold || 30,
            showPanel: options.showPanel !== false,
            historySize: options.historySize || 10,
            updateInterval: options.updateInterval || 500,  // 统一使用500ms
            solutionName: options.solutionName || '未知方案', // 新增方案标识
            ...options
        };

        this.frames = 0;
        this.lastTime = performance.now();
        this.fpsHistory = [];
        this.isRunning = false;
        this.panel = null;

        if (this.options.showPanel) {
            this.createPanel();
        }
    }

    createPanel() {
        this.panel = document.createElement('div');
        this.panel.style.cssText = `
            position: fixed;
            top: 10px;
            right: 10px;
            background: rgba(0,0,0,0.7);
            color: #fff;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            z-index: 10000;
        `;
        document.body.appendChild(this.panel);
    }

    updatePanel(fps, avgFps) {
        if (!this.panel) return;

        this.panel.innerHTML = `
            方案: ${this.options.solutionName}<br>
            当前FPS: ${fps}<br>
            平均FPS: ${avgFps}<br>
            内存: ${Math.round(performance.memory?.usedJSHeapSize / 1048576 || 0)}MB
        `;

        if (fps < this.options.warningThreshold) {
            this.panel.style.backgroundColor = 'rgba(255,0,0,0.7)';
        } else {
            this.panel.style.backgroundColor = 'rgba(0,0,0,0.7)';
        }
    }

    checkFPS = () => {
        if (!this.isRunning) return;

        this.frames++;
        const currentTime = performance.now();
        
        if (currentTime - this.lastTime >= this.options.updateInterval) {
            const fps = Math.round(this.frames * 1000 / (currentTime - this.lastTime));
            
            // 更新FPS历史记录
            this.fpsHistory.push(fps);
            if (this.fpsHistory.length > this.options.historySize) {
                this.fpsHistory.shift();
            }
            
            // 计算平均FPS
            const avgFps = Math.round(
                this.fpsHistory.reduce((a, b) => a + b) / this.fpsHistory.length
            );
            
            // 性能警告
            if (fps < this.options.warningThreshold) {
                console.warn(`性能警告：当前FPS为${fps}，界面可能出现卡顿`);
            }

            // 更新面板
            this.updatePanel(fps, avgFps);

            // 触发回调
            if (this.options.onFPSUpdate) {
                this.options.onFPSUpdate({ fps, avgFps });
            }

            this.frames = 0;
            this.lastTime = currentTime;
        }
        
        requestAnimationFrame(this.checkFPS);
    }

    start() {
        if (!this.isRunning) {
            this.isRunning = true;
            this.checkFPS();
        }
    }

    stop() {
        this.isRunning = false;
        if (this.panel) {
            this.panel.remove();
            this.panel = null;
        }
    }
}

// 使用示例：
/*
const monitor = new PerformanceMonitor({
    warningThreshold: 30,        // FPS警告阈值
    showPanel: true,             // 是否显示面板
    historySize: 10,            // FPS历史记录大小
    onFPSUpdate: ({ fps, avgFps }) => {
        // FPS更新时的回调
        console.log(`FPS: ${fps}, 平均FPS: ${avgFps}`);
    }
});

// 启动监测
monitor.start();

// 停止监测
// monitor.stop();
*/